package baiqi.sishen.community.controller;

import baiqi.sishen.community.dto.CommentDTO;
import baiqi.sishen.community.dto.QuestionDTO;
import baiqi.sishen.community.enums.CommentTypeEnum;
import baiqi.sishen.community.model.Question;
import baiqi.sishen.community.service.CommentService;
import baiqi.sishen.community.service.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/4/30
 */
@Controller
public class QuestionController {

    @Resource
    private QuestionService questionService;

    @Resource
    private CommentService commentService;

    /**
     * 根据id 查找问题
     *
     * @param id    问题id
     * @param model model
     * @return String
     */
    @GetMapping("/question/{id}")
    public String getQuestion(
            @PathVariable("id") Long id, Model model) {

        //增加阅读数
        questionService.incViewCount(id);
        QuestionDTO question = questionService.getQuestionById(id);

        //获取问题评论
        List<CommentDTO> comments = commentService.getCommentsByParentId(id, CommentTypeEnum.QUESTION);

        //获取相关问题
        List<Question> relatedQuestions = questionService.selectRelated(question);
        model.addAttribute("question", question);
        model.addAttribute("comments", comments);
        model.addAttribute("relatedQuestions", relatedQuestions);
        return "question";
    }
}
